#ifndef _P2P_MESSAGE_H_
#define _P2P_MESSAGE_H_

#include "p2p-piece.h"
#include "p2p-peer.h"

#define PSTRLEN 3
#define PSTRLEN_LEN 1
#define PSTR P2P

#define BITFIELD_MSG 5
#define HAVE_MSG 4
#define REQUEST_MSG 6
#define PIECE_MSG 7
#define CANCEL_MSG 8

#define ID_LEN 1
#define LEN_LEN 4
#define INDEX_LEN 4
#define BEGIN_LEN 4
#define LENGTH_LEN 4
#define REQUEST_MSG_LEN (ID_LEN + INDEX_LEN + BEGIN_LEN + LENGTH_LEN)
#define PIECE_MSG_LEN (ID_LEN + INDEX_LEN + BEGIN_LEN)
#define HAVE_MSG_LEN (ID_LEN + INDEX_LEN)
#define HANDSHAKE_MSG_LEN (PSTRLEN_LEN + PSTRLEN)

int handle_handshake_msg(int sockfd, const char *recv_buf, int bytes_recvd);
int handle_request_msg(int sockfd, const char *buf, block_t *block); 
int handle_bitfield_msg(int sockfd, const char *recv_buf, peer_t *peer, const uint8_t *piece_map, int no_of_pieces);
int handle_have_msg(int sockfd, const char *recv_buf, peer_t *peer, const uint8_t *piece_map);
int handle_piece_msg(int sockfd, const char *buf, block_t *block, int len);
int send_handshake(int sockfd);
int send_bitfield_msg(int sockfd);
int send_piece_msg(int sockfd, const block_t *block);
int send_request_msg(int sockfd, const block_t *block);
int broadcast_have_msg(peer_t *peer, int index_r);
#endif // ifndef _P2P_MESSAGE_H_ 
